package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.data.CustAttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

/**
 * <Description> <br>
 *
 * @author zhouJiao<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2022年2月24日 <br>
 * @see com.iwhalecloud.bss.kite.cucc.service.attr.handler <br>
 * @since R9.0<br>
 */

//云聚节点-节点接入带宽联动
public class NodeBandWidthModeAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Override
    public void after(KiteAttr attr, KiteInst inst, AttrData data) {
        // 进入产品变更界面-商品信息都不可修改(置灰以下属性)  -- 已在dcpublic PKEY='wlwCate' and stype = '210728701' 中加入 2004141
        if ("2826".equals(data.getServiceOfferId())) {
            inst.getAttr("200001746").setIsEdit(KeyConsts.IFTRUE_F); // 加入已有VPN业务
            inst.getAttr("200001741").setIsEdit(KeyConsts.IFTRUE_F); // VPN业务名称
            inst.getAttr("2_0000003").setIsEdit(KeyConsts.IFTRUE_F); // 业务模式
            inst.getAttr("200003238").setIsEdit(KeyConsts.IFTRUE_F); // 销售模式
            inst.getAttr("200000003").setIsEdit(KeyConsts.IFTRUE_F); // 节点类型
            operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr);
        }
        super.after(attr, inst, data);
    }

    @Override
    public void onShow(KiteAttr attr, KiteInst inst, AttrData data) {
        if ("2826".equals(data.getServiceOfferId())) {
            // 进入产品变更-带宽变更  节点接入带宽可编辑
            List<String> serviceOfferIds = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "BUSI_CHANGE_SERVICEOFFERID"); // 获取当前服务动作
            if (KiteListUtils.isNotEmpty(serviceOfferIds) && "1059".equals(serviceOfferIds.get(0))) {
                attr.setIsEdit(KeyConsts.IFTRUE_T);
            } else {
                // 取消操作类型则值还原且置灰
                String attrValue = attr.getOriginValue();
                AttrValue attrValue1 = AttrValueCache.getAttrValue("10001000800073", attrValue); // 节点接入带宽的attrid
                attr.setValue(attrValue);
                attr.setValueDesc(attrValue1.getAttrValueDesc());
                // 置灰
                attr.setIsEdit(KeyConsts.IFTRUE_F);

                String sceneInstId = data.getSceneInstId();
                List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(sceneInstId);

                kiteProdInstList.stream().forEach(prodInst ->{
                    // 节点接入带宽
                    KiteAttr kiteAttr = prodInst.getAttr("200001914");
                    if (Objects.nonNull(kiteAttr) && !KiteStringUtils.isEqual(inst.getInstId(), prodInst.getProdInstId())) {
                        List<KiteAttrValue> attrValues = kiteAttr.getAttrValues();
                        if (KiteListUtils.isNotEmpty(attrValues)) {
                            if("10003509".equals(prodInst.getSpecId()) || "63249780".equals(prodInst.getSpecId())){ // 10003509为云联网专线接入产品-新版的prodId
                                operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), attrValue, sceneInstId, null);
                            }
                        }
                    }
                });

            }
        }
        super.onShow(attr, inst, data);
    }
}
